<?php
namespace model;
use model;
class addressModel extends model{

    public static function get_list( $customer_id ){

        $sql = "SELECT * FROM sh_address WHERE customer_id='" . $customer_id . "';";
        $result = self::$db->query( $sql );
//        return !empty($result->num_rows)?$result->rows:false;
        return $result->rows;

    }

    public static function single( $address_id, $customer_id ){

        $sql = "SELECT * FROM sh_address WHERE id='" . $address_id . "' and customer_id='" . $customer_id . "';";
        $result = self::$db->query( $sql );


        return !empty($result->row)?$result->row:false;

    }

    public static function add( $info ){

        $sql = "INSERT INTO sh_address SET " ." customer_id='" . $info['customer_id'] . "',"
            ." `name`='" . $info['name'] . "',"
            ." telephone='" . $info['telephone'] . "',"
            ." detail_address='" . $info['detail_address'] . "',"
            ." province_id='" . $info['province_id'] . "',"
            ." province_name='" . $info['province_name']. "',"
            ." city_id='" . $info['city_id'] . "',"
            ." city_name='" . $info['city_name'] . "',"
            ." area_id='" . $info['area_id'] . "',"
            ." area_name='" . $info['area_name'] . "',"
            ." is_default='" . $info['is_default'] . "'";

        $result = self::$db->query( $sql );
        return $result;
    }

    public static function edit( $id, $info ){

        $sql = "update  sh_address SET "
            ." customer_id='" . $info['customer_id'] . "',"
            ." `name`='" . $info['name'] . "',"
            ." telephone='" . $info['telephone'] . "',"
            ." detail_address='" . $info['detail_address'] . "',"
            ." province_id='" . $info['province_id']. "',"
            ." province_name='" . $info['province_name'] . "',"
            ." city_id='" . $info['city_id'] . "',"
            ." city_name='" . $info['city_name'] . "',"
            ." area_id='" . $info['area_id'] . "',"
            ." area_name='" . $info['area_name'] . "',"
            ." is_default='" . $info['is_default'] . "'"
            ." where id='" . $id . "'";

        ;

        $result = self::$db->query( $sql );
        return $result->num_rows;
    }

    public function remove( $id, $customer_id ){
        $sql = 'select * from sh_address where id="' . $id
            . '" and customer_id="' . $customer_id . '"';

        return self::$db->query( $sql );

    }

    public static function getDefault( $customer_id ){

        $sql = 'select * from sh_address where is_default=1'
            . ' and customer_id="' . $customer_id .'"';
        $ret = self::$db->query( $sql );
        return $ret->row;


    }

    public static function setDefault( $id, $customer_id ){

        $sql = 'update sh_address set is_default=0 '
            . ' and customer_id="' . $customer_id . '"';
        self::$db->query( $sql );
        $sql = 'update sh_address set is_default=1 where id="' . $id
            . '" and customer_id="' . $customer_id .'"';
        return self::$db->query( $sql );

    }

}